package com.jinlejia.erp.ui.mine.contract

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.jinlejia.erp.R
import com.jinlejia.erp.adapter.ProcessTrackingAdapter
import com.jinlejia.erp.base.ApiConfig
import com.jinlejia.erp.base.MapCallBack
import com.jinlejia.erp.databinding.ActivityContractProgressTrackingBinding
import com.jinlejia.erp.utils.DataFilter
import com.zhy.http.okhttp.OkHttpUtils
import okhttp3.Call
import java.util.*

/***
 * 合同进程跟踪Activity
 */
class ContractProgressTrackingActivity : AppCompatActivity() {
    val binding: ActivityContractProgressTrackingBinding by lazy {
        DataBindingUtil.setContentView<ActivityContractProgressTrackingBinding>(
            this,
            R.layout.activity_contract_progress_tracking
        )
    }
    private var adapter: ProcessTrackingAdapter? = null
    private var data = ArrayList<java.util.HashMap<String, Any>>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        getData()
    }

    private fun initView() {

        adapter = ProcessTrackingAdapter(data)
        binding.backBtn.setOnClickListener { finish() }
        binding.recycler.layoutManager = LinearLayoutManager(this)
        binding.recycler.setHasFixedSize(true)
        binding.recycler.adapter = adapter

        adapter!!.setDetailsListener(object : ProcessTrackingAdapter.OnDetailsListener {
            override fun onClick(trackId: Int) {
                intent = Intent(baseContext, FollowUpDetailsListActivity::class.java)
                intent.putExtra("trackId", trackId)
                startActivity(intent)
            }

        })
    }

    private fun getData() {
        OkHttpUtils.get()
            .url(ApiConfig.Progress + intent.getStringExtra("pactCode"))
            .build()
            .execute(object : MapCallBack(){
                override fun onResponse(response: MutableMap<String, Any>?, id: Int) {
                    val filter = DataFilter.filter(baseContext,response,adapter)
                    if (filter){
                        data = JSON.parseObject(
                            response!!["data"].toString(),
                            object :
                                TypeReference<ArrayList<HashMap<String, Any>>>() {})
                        adapter!!.data = data
                    }
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                   e!!.printStackTrace()
                }

            })


    }


}
